package com.dc.service.Impl;

import com.dc.dao.TagRepository;
import com.dc.handler.NotFoundException;
import com.dc.po.Tag;
import com.dc.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagRepository tagRepository;

    @Transactional
    @Override  //新增或保存
    public Tag saveTag(Tag tag) {
        //业务性校验，名称不能重复
        Tag tagByName = getTagByName(tag.getName());
        if(tagByName!=null){
            return null;
        }
        //业务性校验，检查表中是否有该id
        if (tag.getId() != null) {
            Tag t = tagRepository.getOne(tag.getId());
            if (t == null) {
                throw new NotFoundException("不存在该id类型");
            }
        }
        //有id则更新，无id则新增
        return tagRepository.save(tag);
    }

    @Override
    public Tag getTag(Long id) {
        return tagRepository.getOne(id);
    }

    @Override
    public Tag getTagByName(String name) {
        return tagRepository.findByName(name);
    }

    @Override
    public Page<Tag> listTag(Pageable pageable) {
        return tagRepository.findAll(pageable);
    }

    @Override
    public List<Tag> listTag() {
        return tagRepository.findAll();
    }

    @Override
    public List<Tag> listTagTop(Integer size) {
        Sort sort = Sort.by(Sort.Direction.DESC, "blogs.size");
        Pageable pageable = PageRequest.of(0, size, sort);
        return tagRepository.findTop(pageable);
    }

    @Override //ids = "1,2,3"或者"1,2,3,"error"
    public List<Tag> listTag(String ids) {
        if("".equals(ids)||ids.length()==0){
            return null;
        }
        return tagRepository.findAllById(checkNewTagAndToList(ids));
    }

    private List<Long> convertToList(String ids){
        List<Long> list = new ArrayList<>();
        if(!"".equals(ids)&&ids != null){
            String[] idArray = ids.split(",");
            for (String id : idArray) {
                list.add(new Long(id));
            }
        }
        return list;
    }

    //判断前端传来的标签ids字符串是否含有新的标签，有就保存新标签并生成新的id集合
    private List<Long> checkNewTagAndToList(String ids) {
        List<Long> list = new ArrayList<>();
        if (!"".equals(ids) && ids != null) {
            String[] idArray = ids.split(",");
            for (String id : idArray) {
                boolean isNum = Pattern.compile("[0-9]*").matcher(id).matches();
                if (isNum) {
                    if (tagRepository.getOne(new Long(id)) == null) {
                        Tag t = new Tag();
                        t.setName(id);
                        Tag tag = this.saveTag(t);
                        if (tag != null) {
                            list.add(tag.getId());
                        }
                    } else {
                        list.add(new Long(id));
                    }
                } else {
                    Tag t = new Tag();
                    t.setName(id);
                    Tag tag = this.saveTag(t);
                    if (tag != null) {
                        list.add(tag.getId());
                    }
                }
            }
        }
        return list;
    }



    @Transactional
    @Override
    public Tag updateTag(Long id, Tag tag) {
        //检验该id在表中是否存在
        Tag t = tagRepository.getOne(id);
        if (t == null) {
            throw new NotFoundException("不存在该id类型");
        }
        return tagRepository.save(tag);//tag中没id执行新增，有id就修改
    }

    @Transactional
    @Override
    public void deleteTag(Long id) {
        tagRepository.deleteById(id);
    }


}


